package com.jiutong.meipengdai.ui.activitys;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.base.BaseActivity;
import com.jiutong.meipengdai.entity.BaseRespond;
import com.jiutong.meipengdai.entity.IdentifyCodeRespond;
import com.jiutong.meipengdai.http.model.interfaces.RegisterAndLoginBiz;
import com.jiutong.meipengdai.http.model.requests.CheckPhoneNumRequest;
import com.jiutong.meipengdai.http.present.UserInfoPresent;
import com.jiutong.meipengdai.http.subscribers.ProgressSubscriber;
import com.jiutong.meipengdai.http.subscribers.SubscriberOnNextListener;
import com.jiutong.meipengdai.utils.StringUtil;
import com.jiutong.meipengdai.utils.TimerUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by suxi on 2017/5/27.
 */

public class RegisterActivity extends BaseActivity implements RegisterAndLoginBiz {
    @Bind(R.id.ed_phone_num)
    EditText edPhoneNum;
    @Bind(R.id.shield)
    ImageView shield;
    @Bind(R.id.ed_identify_code)
    EditText edIdentifyCode;
    @Bind(R.id.ed_password)
    EditText edPassword;
    @Bind(R.id.btn_register)
    Button btnRegister;
    @Bind(R.id.tv_login)
    TextView tv_login;
    @Bind(R.id.tv_vercode)
    TextView tvVercode;
    @Bind(R.id.tv_agreement)
    TextView tv_agreement;

    private UserInfoPresent userInfoPresent;


    private String identifyCode;

    private TimerUtil timerUtil;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        //初始化倒计时
        timerUtil = new TimerUtil(tvVercode, edIdentifyCode);
        timerUtil.setCountTimerEt();


        userInfoPresent = new UserInfoPresent(this, this);
    }

    @Override
    public String getPhoneNum() {
        return edPhoneNum.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return edPassword.getText().toString().trim();
    }

    @Override
    public String getVerificatCode() {
        return edIdentifyCode.getText().toString().trim();
    }

    @Override
    public String getOriPwd() {
        return null;
    }

    @Override
    public String getNewPwd() {
        return null;
    }

    @Override
    public String getRelName() {
        return null;
    }

    @Override
    public String getIdCard() {
        return null;
    }

    @Override
    public String getEmail() {
        return null;
    }

    @Override
    public String getNewPhone() {
        return null;
    }

    @Override
    public String getCardNumber() {
        return null;
    }

    @Override
    public void onVerCodeGetSuccess(IdentifyCodeRespond subjects) {
        if(subjects.getResCode().equals("200")){
            timerUtil.startTimer();
            identifyCode = subjects.getData();
        }else{
            toastShortShow(subjects.getResDesc());
        }


    }

    @Override
    public void onSuccess(Object object) {
        BaseRespond respond = (BaseRespond) object;
        if (respond.getResCode().equals("200")) {
            toastShortShow(respond.getResDesc());
//        SpConfig.getInstance().putString("sessionId", respond.getSessionId());
            openActivity(LoginActivity.class);
            finish();
        } else {
            toastShortShow(respond.getResDesc());
        }


    }

    @Override
    public void onError(Throwable e) {

    }

    @OnClick({R.id.btn_register, R.id.tv_login, R.id.tv_vercode, R.id.tv_agreement})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_register:
                if (isRightAction())
                    userInfoPresent.register();
                break;
            case R.id.tv_login:
                openActivity(LoginActivity.class);
                break;
            case R.id.tv_vercode:
                if (isRightPhone()) {
                    isPhoneOk();
                }
                break;
            case R.id.tv_agreement:  //美鹏贷协议
                openActivity(AgreementActivity.class);
                break;
        }
    }

    private SubscriberOnNextListener onNextListener;
    private HashMap<String, Object> fieldsMap;
    private ProgressSubscriber progressSubscriber;
    private CheckPhoneNumRequest checkPhoneNumRequest;

    private void isPhoneOk() {
        if (checkPhoneNumRequest == null) {
            checkPhoneNumRequest = new CheckPhoneNumRequest();
        }
        onNextListener = new SubscriberOnNextListener<BaseRespond>() {
            @Override
            public void onNext(BaseRespond object) {
                BaseRespond respond = (BaseRespond) object;
                if (respond.getResCode().equals("200")) {
                    userInfoPresent.getAuthCode();
                } else {
                    toastShortShow(respond.getResDesc());
                }

            }

            @Override
            public void onError(Throwable e) {

            }
        };
        fieldsMap = new HashMap<>();
        fieldsMap.put("phoneNo", getPhoneNum());
        progressSubscriber = new ProgressSubscriber(onNextListener, mContext);
        checkPhoneNumRequest.checkPhoneNum(progressSubscriber, fieldsMap);

    }

    /**
     * 手机号是否输入正确
     */
    private boolean isRightPhone() {
        if (TextUtils.isEmpty(getPhoneNum())) {
            toastShortShow("请输入手机号码");
            return false;
        } else if (!StringUtil.isMobileNumber(getPhoneNum())) {
            toastShortShow("手机号码格式有误，请输入正确手机号码");
            return false;
        }
        return true;
    }

    /**
     * 操作是否正确
     *
     * @return
     */
    private boolean isRightAction() {
        if (!isRightPhone()) {
            return false;
        }

        if (TextUtils.isEmpty(getVerificatCode())) {
            toastShortShow("请输入手机验证码");
            return false;
        }

        if (TextUtils.isEmpty(getPassword())) {
            toastShortShow("请输入密码");
            return false;
        }

        if (getPassword().length() < 8 || getPassword().length() > 16) {
            toastShortShow("请输入8-16位数的密码");
            return false;
        }

        if (TextUtils.isEmpty(identifyCode) || !identifyCode.equals(getVerificatCode())) {
            toastShortShow("您输入的验证码不正确,请重新输入");
            return false;
        }
        return true;
    }

}
